package controller;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gdata.util.ServiceException;

import redstone.xmlrpc.XmlRpcFault;
import server.TestBlogger;
import server.TestCustomUrl;
import server.TestWp;

public class TestInfoServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		response.setContentType("html/plain");
		String type = request.getParameter("type");
		if(type!=null&&type.equals("wordpress"))
		{
			String blogurl = request.getParameter("blogurl");
			if("".equals(blogurl))
				blogurl = "a";
			String usernameblog = request.getParameter("usernameblog");
			String passwordblog = request.getParameter("passwordblog");
			blogurl = Utils.fomatSitemap(blogurl);
			try {
	        	String result = TestWp.postWp(usernameblog, passwordblog, blogurl);
	        	if(!result.equals(""))
	        		response.getWriter().println("1");
	        	else
	        		response.getWriter().println("2");
			} catch (XmlRpcFault e) {
				response.getWriter().println("2");
//				e.printStackTrace();
			}
		}
		if(type!=null&&type.equals("blogger"))
		{
			String blogurl = request.getParameter("blogurl");
			String email = request.getParameter("email");
			String passwordemail = request.getParameter("passwordemail");
			String blogid = request.getParameter("blogid");
			
			blogurl = Utils.fomatSitemap(blogurl);
			try {
	        
	        	TestBlogger.postBlogger(email, passwordemail,blogid, blogurl);

	        	response.getWriter().println("1");

			} catch (ServiceException e) {
				response.getWriter().println("2");
				e.printStackTrace();
			}
		}
		if(type!=null&&type.equals("customurl"))
		{
			String customurlurl = request.getParameter("customurlurl");
			String usernamecustomurl = request.getParameter("usernamecustomurl");
			String passwordcustomurl = request.getParameter("passwordcustomurl");
			
			String str = TestCustomUrl.post(usernamecustomurl, passwordcustomurl,customurlurl);
			System.out.println(str);
			if("-1".equals(str))
				response.getWriter().println("2");
			else
				response.getWriter().println("1");
		}
	}
}
